# encoding:utf-8

from py2neo import Graph, Node, Relationship
import xlrd
import statsmodels.api as sm

rDrives = Relationship.type("DRIVES")
rSubordinate = Relationship.type("SUBORDINATES")
rSubClass = Relationship.type("SUBCLASS")
rProxy = Relationship.type("PROXY")
rProperty = Relationship.type("PROPERTY")
rProfiling = Relationship.type("PROFILING")
rValue = Relationship.type("VALUE")
rInstanceOf = Relationship.type("INSTANCE")
rAct = Relationship.type("ACT")
rAffect = Relationship.type("Affect")


def GenerateNodes():
    n = {}

    # -------------------------------------------------------------------------------------
    # ---Basic---
    n["Economy"] = Node("Class", name="经济", english="Economy")
    n["Business"] = Node("Class", name="工商业活动", english="Business")
    n["CentralBank"] = Node("Entity", name="中央银行", english="Central Bank")
    n["Government"] = Node("Entity", name="政府", english="Government")
    n["PriceLevel"] = Node("Class", name="价格指数", english="Price Level")
    n["IR"] = Node("Class", name="利率", english="Interest Rate")
    n["Bank"] = Node("Entity", name="银行", english="银行")
    #
    n["InterBankMarket"] = Node("Class", name="银行间市场货币供给", english="InterBank Market")
    #
    n["AssetPrice"] = Node("Class", name="资产价格", english="Asset Price")
    n["Stock"] = Node("Class", name="股票", english="Stock")

    # --------------------------------------------------------------------------------------
    # ---Economy---
    n["GDP"] = Node("Indicator", name="GDP", english="GDP")
    n["KeqiangIndicator"] = Node("Indicator", name="可强指数", english="Keqiang Indicator")

    # ---Cycle---
    n["DebtCycle"] = Node("Class", name="债务周期", english="Debt Cycle")
    n["BusinessCycle"] = Node("Class", name="商业周期", english="Business Cycle")
    n["InventoryCycle"] = Node("Class", name="库存周期", english="Inventory Cycle")

    # ---Cobb Douglas Production---
    n["CDLaborInput"] = Node("Class", name="CD劳动力输入", english="Labor Input")
    n["CDCapitalInput"] = Node("Class", name="CD资本输入", english="Capital Input")
    n["CDTFP"] = Node("Class", name="CD要素生产力", english="Total Factor Productivity")

    # ---Drivers---
    n["Export"] = Node("Class", name="经济驱动：出口", english="Export")
    n["Consumption"] = Node("Class", name="经济驱动：消费", english="Consumption")
    n["Investment"] = Node("Class", name="经济驱动：投资", english="Investment")
    n["RealEstate"] = Node("Class", name="经济驱动：房地产", english="RealEstate")

    # ---------------------------------------------------------------------------------------
    # ---Government---
    n["Fiscal"] = Node("Class", name="财政政策", english="Fiscal Policy")
    n["CCPConference"] = Node("Class", name="中央经济会议", english="CCP Eco Conference")
    n["SupplyReform"] = Node("Class", name="供给侧改革", english="Supply Side Reform")
    n["CountryRisk"] = Node("Class", name="国家风险", english="Country Risk")
    n["MundellianTrilemma"] = Node("Class", name="三元悖论", english="Mundellian Trilemma")

    # ---------------------------------------------------------------------------------------
    # ---Central Bank---
    n["MonetaryPolicy"] = Node("Class", name="货币政策", english="Monetary Policy")
    n["CBOpenMarket"] = Node("Action", name="公开市场操作", english="Open Market")
    n["CBOperation"] = Node("Action", name="逆周期操作", english="CB Operation")
    n["CBBS"] = Node("Class", name="央行资产负债表", english="CB Balance Sheet")
    n["TaylorRule"] = Node("Class", name="泰勒法则", english="Taylor Rule")
    n["CBPolicyReport"] = Node("Class", name="央行货币政策执行报告", english="CB Monetary Policy Report")

    #
    n["CBOperation_DecreaseRate"] = Node("Event", name="央行降息", english="CB Operation")
    n["TBillIssue"] = Node("Action", name="央票发行", english="T-Bill Issue")
    n["CBBondTrading"] = Node("Action", name="央行现券交易", english="CB Bond Trading")
    n["CBRepo"] = Node("Action", name="央行回购", english="CB Repo")

    # ---Interest Rate---
    n["STIR"] = Node("Indicator", name="短端利率", english="S-T Interest Rate")
    n["LTIR"] = Node("Indicator", name="长端利率", english="L-T Interest Rate")

    n["BMRate"] = Node("Indicator", name="基准利率", english="Benchmark Interest Rate")
    n["BankReserveRate"] = Node("Indicator", name="存款准备金", english="Bank Reserve Rate")
    n["BankExtraReserveRate"] = Node("Indicator", name="银行超储率", english="Bank Reserve Rate")

    # ---------------------------------------------------------------------------------------
    # ---Price Level---
    n["CreditExpansion"] = Node("Action", name="信用扩张", english="Credit Expansion")
    n["SocialFinancing"] = Node("Indicator", name="社会融资总额", english="Social Financing")
    n["SocialLiquidity"] = Node("Class", name="实体间流动性", english="Social Liquidity")
    n["CompanyFinancing"] = Node("Class", name="企业融资", english="Company Financing")
    n["SmallDebtFinancing"] = Node("Indicator", name="小微贷款量", english="Company Financing")
    n["WeightedDebtRate"] = Node("Indicator", name="人民币加权平均贷款利率", english="Weighted DebtRate")

    n["CPI"] = Node("Indicator", name="CPI", english="CPI")
    n["PPI"] = Node("Indicator", name="PPI", english="PPI")

    # ----------------------------------------------------------------------------------------
    # ---Commodity---
    n["FarmProduce"] = Node("Class", name="农产品", english="Farm Produce")
    n["LiveStock"] = Node("Class", name="牲畜", english="Live Stock")
    n["Metal"] = Node("Class", name="工业金属", english="Metal")
    n["PreciousMetal"] = Node("Class", name="贵金属", english="PreciousMetal")
    n["CrudeOil"] = Node("Class", name="原油", english="Crude Oil")

    # ----------------------------------------------------------------------------------------
    # ---Industry---
    n["Industry"] = Node("Class", name="行业", english="Industry")
    n["Industry_Banking"] = Node("Class", name="医药制造", english="Industry_Banking")
    n["Industry_Insurance"] = Node("Class", name="保险", english="Industry_Insurance")
    n["Industry_Drug"] = Node("Class", name="医药制造", english="Industry_Drug")
    n["Industry_Securities"] = Node("Class", name="券商", english="Industry_Securities")
    n["Industry_Trust"] = Node("Class", name="信托", english="Industry_Trust")
    n["Industry_Oil"] = Node("Class", name="石油化工", english="Industry_Oil")
    n["Industry_EE"] = Node("Class", name="电子元器件", english="Industry_EE")
    n["Industry_RealEstate"] = Node("Class", name="房地产", english="Industry_RealEstate")
    n["Industry_Alcohol"] = Node("Class", name="酿酒行业", english="Industry_Alcohol")
    n["Industry_Auto"] = Node("Class", name="汽车行业", english="Industry_Auto")
    n["Industry_Software"] = Node("Class", name="软件服务", english="Industry_Software")

    # ---房地产行业---
    n["Industry_Residence"] = Node("Class", name="住宅地产", english="Industry_Residence")
    n["Industry_CommercialRealEstate"] = Node("Class", name="商业地产", english="Industry_CommercialRealEstate")
    n["Industry_IndustrialRealEstate"] = Node("Class", name="产业地产", english="Industry_IndustrialRealEstate")
    n["Industry_CommercialRealEstateOperation"] = Node("Class", name="商业地产运营",
                                                       english="Industry_CommercialRealEstateOperation")
    n["Industry_ApartmentOperation"] = Node("Class", name="长租公寓运营", english="Industry_ApartmentOperation")
    n["Industry_PropertyManagement"] = Node("Class", name="物业管理", english="Industry_PropertyManagement")
    n["Industry_RealEstateAgent"] = Node("Class", name="房地产中介", english="Industry_RealEstateAgent")
    # ---建筑行业---
    n["Industry_Construction"] = Node("Class", name="建筑", english="Industry_Construction")
    n["Industry_BuildingDesign"] = Node("Class", name="建筑设计", english="Industry_BuildingDesign")
    n["Industry_BuildingConstruction"] = Node("Class", name="房屋建筑", english="Industry_BuildingConstruction")
    n["Industry_Decoration"] = Node("Class", name="装修装饰", english="Industry_Decoration")
    n["Industry_Infrastructure"] = Node("Class", name="基础设施建设", english="Industry_Infrastructure")
    n["Industry_Landscaping"] = Node("Class", name="园林与绿化工程", english="Industry_Landscaping")
    # ---建材---
    n["Industry_BuildingMaterials"] = Node("Class", name="建材", english="Industry_BuildingMaterials")
    n["Industry_Glass"] = Node("Class", name="玻璃", english="Industry_Glass")
    n["Industry_Cement"] = Node("Class", name="水泥", english="Industry_Cement")
    n["Industry_Steel"] = Node("Class", name="钢铁", english="Industry_Steel")
    # ---基本金属---
    n["Industry_Metal"] = Node("Class", name="基本金属", english="Industry_Metal")
    n["Industry_Copper"] = Node("Class", name="铜", english="Industry_Zinc")
    n["Industry_Zinc"] = Node("Class", name="锌", english="Industry_Zinc")
    n["Industry_Alum"] = Node("Class", name="铝", english="Industry_Alum")
    # ---下游消费---
    n["Industry_HouseHolding"] = Node("Class", name="家用家居", english="Industry_Furniture")
    n["Industry_Furniture"] = Node("Class", name="家具", english="Industry_Furniture")
    n["Industry_HomeAppliances"] = Node("Class", name="家用电器", english="Industry_HomeAppliances")

    # n["Industry_Residence"] = Node("Entity", name="土地", english="Industry_Software")

    # -----------------------------------------------------------------------------------------
    # ---Stock Fundamental Analysis
    n["FundamentalPrice"] = Node("Class", name="基本面估值", english="Fundamental Price")

    # ---Earning Analysis
    n["Earning"] = Node("Class", name="公司盈利", english="Earning")

    # ---Earning Analysis --- 商业分析
    n["BrandValue"] = Node("Class", name="品牌价值", english="Brand Value")

    # ---Earning Analysis --- 商业分析---其他：规模化 poter 5 Power，技术突破，收入结构是否发生变化

    # ---Earning Analysis --- 经营分析 (DuPont Based)
    # n["IncomeAnalysis"] = Node("Class", name="收益分析", english="Income Analysis")
    n["CorpIncome"] = Node("Indicator", name="收入", english="Corp Income")
    n["CorpCost"] = Node("Indicator", name="成本", english="Corp Cost")
    n["ProfitMargin"] = Node("Indicator", name="利润率", english="Profit Margin")
    n["AssetTurnover"] = Node("Indicator", name="资产周转率", english="Asset Turnover")
    n["Leverage"] = Node("Indicator", name="杠杆率", english="Leverage")
    n["ROA"] = Node("Indicator", name="资产收益率", english="ROA")
    n["ROE"] = Node("Indicator", name="净资产收益率", english="ROE")

    # ---Earning Analysis --- 增长因子
    n["ProfitGrowth"] = Node("Indicator", name="利润增长", english="ProfitGrowth")

    # ---Earning Analysis --- 现金流
    n["CashFlow"] = Node("Indicator", name="现金流", english="Corp Cash Flow")

    # ---Earning Analysis --- RedFlag

    # ---Valuation Analysis
    n["StockValuation"] = Node("Indicator", name="估值因子", english="Valuation")

    # ----------------------------------------------------------------------------------------------
    # ---Stock---
    # Business Operation
    n["Corporation"] = Node("Entity", name="公司", english="Corporation")
    n["BusinessOperation"] = Node("Class", name="公司经营", english="BusinessOperation")
    n["CostOfDebt"] = Node("Indicator", name="债务成本", english="Cost Of Debt")

    # 价格分析：动量与翻转
    n["Momentum"] = Node("Indicator", name="动量因子", english="Momentum")
    # 狭义流动性分析：投资者结构
    n["InvestorStructure"] = Node("Class", name="投资者结构", english="Investor Structure")
    # 事件驱动
    n["EventDriven"] = Node("Class", name="事件驱动", english="Event Driven")
    # ---Capital Restructure---
    n["Restructure"] = Node("Class", name="资产重组", english="Restructure")
    n["DividendStock"] = Node("Class", name="股票送转", english="Dividend Stocks")

    # -----------------------------------------------------------------------------------------------
    # ---Fix Income---
    n["FixIncome"] = Node("Class", name="固定收益", english="FixIncome")

    # ------------------------------------------------------------------------------------------------
    # ---Global---
    n["GlobalTrade"] = Node("Class", name="全球贸易", english="Economy")
    n["Globalization"] = Node("Class", name="全球化", english="Economy")
    n["BeltAndRoad"] = Node("Class", name="一带一路", english="Economy")
    n["TradeConflict"] = Node("Class", name="贸易摩擦", english="Economy")
    n["RMBInternationalization"] = Node("Class", name="人民币国际化", english="RMBInternationalization")
    n["FXCapitalFlow"] = Node("Class", name="FX资本流动", english="FXCapitalFlow")
    n["FXRiskOnOff"] = Node("Class", name="避险情绪", english="FXRiskOnOff")
    n["USEconomy"] = Node("Class", name="美国经济", english="FXRiskOnOff")
    n["RMB"] = Node("Indicator", name="人民币", english="RMB")
    n["USDX"] = Node("Class", name="美元指数", english="USDX")

    #
    return n


n = GenerateNodes()


def PrintGraph(graph):
    relations = graph.relationships.match()
    for relation in relations:
        print(relation)

    #
    nodes = graph.nodes.match()
    for no in nodes:
        s = str(no)
        print(s)


def ExportToFile(graph, pathFilename):
    file = open(pathFilename, 'w', encoding='UTF-8')
    content = ""

    relations = graph.relationships.match()
    for relation in relations:
        content += str(relation) + "\n"

    #
    nodes = graph.nodes.match()
    for no in nodes:
        content += str(no) + "\n"

    #
    file.write(content)
    file.close()


def Clear(graph):
    graph.delete_all()


def AddConceptRelation(graph, nodeA, relation, nodeB):
    graph.merge(relation(nodeA, nodeB), "Person", "name")


def BuildBigPicture(graph, n):
    #
    BuildEconomy(graph, n)

    #
    BuildGovernmentAction(graph, n)

    #
    BuildCentralBankAction(graph, n)

    #
    BuildPriceLevel(graph, n)

    #
    BuildCommodityAnalysis(graph, n)

    #
    BuildIndustryAnalysis(graph, n)
    #

    BuildStockAnalysis(graph, n)

    #
    BuildFixIncomeAnalysis(graph, n)

    #
    BuildGlobalEconomy(graph, n)


def BuildEconomy(graph, n):
    # ---
    graph.merge(rProxy(n["GDP"], n["Economy"]), "Class", "name")
    graph.merge(rProxy(n["KeqiangIndicator"], n["Economy"]), "Class", "name")

    # ---Cycle---
    graph.merge(rProfiling(n["DebtCycle"], n["Economy"]), "Class", "name")
    graph.merge(rProfiling(n["BusinessCycle"], n["Economy"]), "Class", "name")
    graph.merge(rProfiling(n["InventoryCycle"], n["Economy"]), "Class", "name")

    # ---Cobb Douglas Production---
    # graph.merge(rDrives(n["CDLaborInput"], n["Economy"]), "Class", "name")
    # graph.merge(rDrives(n["CDCapitalInput"], n["Economy"]), "Class", "name")
    # graph.merge(rDrives(n["CDTFP"], n["Economy"]), "Class", "name")

    # ---Drivers---
    graph.merge(rDrives(n["Export"], n["Economy"]), "Class", "name")
    graph.merge(rDrives(n["Consumption"], n["Economy"]), "Class", "name")
    graph.merge(rDrives(n["Investment"], n["Economy"]), "Class", "name")
    graph.merge(rDrives(n["RealEstate"], n["Economy"]), "Class", "name")

    # ---
    graph.merge(rSubordinate(n["Economy"], n["Business"]), "Class", "name")


def BuildGovernmentAction(graph, n):
    # ---Government---
    graph.merge(rSubordinate(n["RealEstate"], n["Government"]), "Class", "name")
    graph.merge(rSubordinate(n["Investment"], n["Government"]), "Class", "name")

    graph.merge(rDrives(n["Government"], n["Fiscal"]), "Class", "name")
    graph.merge(rDrives(n["Fiscal"], n["Business"]), "Class", "name")

    # 中央经济会议"
    graph.merge(rDrives(n["CCPConference"], n["Government"]), "Class", "name")

    # "供给侧改革"
    graph.merge(rDrives(n["SupplyReform"], n["Government"]), "Class", "name")

    # "国家风险"
    graph.merge(rDrives(n["CountryRisk"], n["Government"]), "Class", "name")

    # "三元悖论"
    graph.merge(rDrives(n["Government"], n["MundellianTrilemma"]), "Class", "name")


def BuildCentralBankAction(graph, n):
    # ---Central Bank---
    graph.merge(rDrives(n["Economy"], n["CentralBank"]), "Class", "name")

    # ---动机---
    # 泰勒法则
    # graph.merge(rSubordinate(n["TaylorRule"], n["CentralBank"]), "Class", "name")
    graph.merge(rDrives(n["PriceLevel"], n["CentralBank"]), "Class", "name")

    # 货币政策
    graph.merge(rProfiling(n["MonetaryPolicy"], n["CentralBank"]), "Class", "name")
    # 央行资产负债表
    graph.merge(rProfiling(n["CBBS"], n["CentralBank"]), "Class", "name")

    #
    # 公开市场操作
    graph.merge(rDrives(n["CentralBank"], n["CBOpenMarket"]), "Class", "name")
    # graph.merge(rDrives(n["CBOpenMarket"], n["InterBankMarket"]), "Class", "name")
    # 现券交易
    graph.merge(rSubClass(n["CBOpenMarket"], n["CBBondTrading"]), "Class", "name")
    graph.merge(rDrives(n["CBBondTrading"], n["InterBankMarket"]), "Class", "name")
    # 央票发行
    graph.merge(rDrives(n["CBOpenMarket"], n["TBillIssue"]), "Class", "name")
    graph.merge(rDrives(n["TBillIssue"], n["InterBankMarket"]), "Class", "name")
    # 央行回购
    graph.merge(rDrives(n["CBOpenMarket"], n["CBRepo"]), "Class", "name")
    graph.merge(rDrives(n["CBRepo"], n["InterBankMarket"]), "Class", "name")

    # 逆周期操作
    graph.merge(rDrives(n["CentralBank"], n["CBOperation"]), "Class", "name")
    # graph.merge(rDrives(n["CBOperation"], n["InterBankMarket"]), "Class", "name")
    #
    graph.merge(rDrives(n["InterBankMarket"], n["Bank"]), "Class", "name")
    graph.merge(rDrives(n["InterBankMarket"], n["AssetPrice"]), "Class", "name")

    # ---Interest Rate---
    # 短期利率
    graph.merge(rProxy(n["STIR"], n["IR"]), "Class", "name")
    graph.merge(rDrives(n["InterBankMarket"], n["STIR"]), "Class", "name")

    # 长期利率
    graph.merge(rProxy(n["LTIR"], n["IR"]), "Class", "name")
    graph.merge(rDrives(n["InterBankMarket"], n["LTIR"]), "Class", "name")

    # 基准利率
    # graph.merge(rDrives(n["CentralBank"], n["BMRate"]), "Class", "name")
    graph.merge(rDrives(n["CBOperation"], n["BMRate"]), "Class", "name")
    graph.merge(rDrives(n["BMRate"], n["Bank"]), "Class", "name")

    # 存款准备金
    # graph.merge(rDrives(n["CentralBank"], n["BankReserveRate"]), "Class", "name")
    graph.merge(rDrives(n["CBOperation"], n["BankReserveRate"]), "Class", "name")
    graph.merge(rDrives(n["BankReserveRate"], n["Bank"]), "Class", "name")

    #
    graph.merge(rDrives(n["BankExtraReserveRate"], n["Bank"]), "Class", "name")


def BuildPriceLevel(graph, n):
    # ---Price Level---
    # 信用扩张
    graph.merge(rDrives(n["Bank"], n["CreditExpansion"]), "Class", "name")
    graph.merge(rDrives(n["CreditExpansion"], n["SocialLiquidity"]), "Class", "name")

    # 社会融资总额
    graph.merge(rProxy(n["SocialFinancing"], n["SocialLiquidity"]), "Class", "name")

    # 实体间流动性
    graph.merge(rProxy(n["WeightedDebtRate"], n["CompanyFinancing"]), "Class", "name")

    # 实体间流动性通过推高 资产价格 而推高PriceLevel
    # graph.merge(rDrives(n["SocialLiquidity"], n["PriceLevel"]), "Class", "name") #
    graph.merge(rDrives(n["SocialLiquidity"], n["AssetPrice"]), "Class", "name")
    graph.merge(rDrives(n["SocialLiquidity"], n["Consumption"]), "Class", "name")

    # 企业融资
    graph.merge(rDrives(n["SocialLiquidity"], n["CompanyFinancing"]), "Class", "name")
    graph.merge(rSubordinate(n["SmallDebtFinancing"], n["CompanyFinancing"]), "Class", "name")

    #
    graph.merge(rProxy(n["CPI"], n["PriceLevel"]), "Class", "name")
    graph.merge(rProxy(n["PPI"], n["PriceLevel"]), "Class", "name")


# ---Commodity---
def BuildCommodityAnalysis(graph, n):
    #
    graph.merge(rDrives(n["AssetPrice"], n["FarmProduce"]), "Class", "name")
    graph.merge(rDrives(n["AssetPrice"], n["LiveStock"]), "Class", "name")
    graph.merge(rDrives(n["AssetPrice"], n["Metal"]), "Class", "name")
    graph.merge(rDrives(n["AssetPrice"], n["PreciousMetal"]), "Class", "name")
    graph.merge(rDrives(n["AssetPrice"], n["CrudeOil"]), "Class", "name")
    #
    graph.merge(rDrives(n["FarmProduce"], n["PriceLevel"]), "Class", "name")
    graph.merge(rDrives(n["LiveStock"], n["PriceLevel"]), "Class", "name")
    graph.merge(rDrives(n["Metal"], n["PriceLevel"]), "Class", "name")
    graph.merge(rDrives(n["CrudeOil"], n["PriceLevel"]), "Class", "name")


def BuildIndustryAnalysis(graph, n):
    # ---Industry Analysis---
    graph.merge(rSubClass(n["Industry_Banking"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Insurance"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Drug"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Securities"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Trust"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Auto"], n["Industry"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_RealEstate"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Residence"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_CommercialRealEstate"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_IndustrialRealEstate"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_CommercialRealEstateOperation"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_ApartmentOperation"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_PropertyManagement"], n["Industry_RealEstate"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_RealEstateAgent"], n["Industry_RealEstate"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_Construction"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_BuildingDesign"], n["Industry_Construction"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_BuildingConstruction"], n["Industry_Construction"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Decoration"], n["Industry_Construction"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Infrastructure"], n["Industry_Construction"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Landscaping"], n["Industry_Construction"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_BuildingMaterials"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Glass"], n["Industry_BuildingMaterials"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Cement"], n["Industry_BuildingMaterials"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_Metal"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Copper"], n["Industry_Metal"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Zinc"], n["Industry_Metal"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Alum"], n["Industry_Metal"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_Steel"], n["Industry"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_HouseHolding"], n["Industry"]), "Class", "name")
    graph.merge(rSubClass(n["Industry_Furniture"], n["Industry_HouseHolding"]), "Class", "name")

    #
    graph.merge(rSubClass(n["Industry_HomeAppliances"], n["Industry"]), "Class", "name")
    pass


def RealEstateAnalysis(graph, n):
    # ---上游---
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_Metal"]), "Class", "name")
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_BuildingMaterials"]), "Class", "name")
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_Construction"]), "Class", "name")
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_Steel"]), "Class", "name")
    # ---下游---
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_HomeAppliances"]), "Class", "name")
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_HouseHolding"]), "Class", "name")
    graph.merge(rDrives(n["Industry_RealEstate"], n["Industry_Auto"]), "Class", "name")


def StockFundamentalPrice(graph, n):
    # -------------------------------------------------------------------
    # 基本面估值 Price = Earning X PE // Aka Price X Growth X Liquidity
    graph.merge(rDrives(n["FundamentalPrice"], n["Stock"]), "Class", "name")

    # ---Earning Analysis 公司盈利
    # graph.merge(rDrives(n["Earning"], n["FundamentalPrice"]), "Class", "name")

    # ---Earning Analysis --- 商业分析
    # 品牌价值
    graph.merge(rDrives(n["BrandValue"], n["Earning"]), "Class", "name")

    # ---Earning Analysis --- 商业分析---其他：规模化 poter 5 Power，技术突破，收入结构是否发生变化

    # ---Earning Analysis --- 经营分析 (DuPont Based)
    # 收益分析"
    # graph.merge(rDrives(n["BusinessOperation"], n["Earning"]), "Class", "name")
    #
    graph.merge(rDrives(n["CorpIncome"], n["ProfitMargin"]), "Class", "name")  # 收入
    graph.merge(rDrives(n["CorpCost"], n["ProfitMargin"]), "Class", "name")  # 成本

    graph.merge(rDrives(n["ProfitMargin"], n["Earning"]), "Class", "name")  # 利润率
    graph.merge(rDrives(n["AssetTurnover"], n["Earning"]), "Class", "name")  # 资产周转率
    graph.merge(rDrives(n["Leverage"], n["Earning"]), "Class", "name")  # 杠杆率

    # graph.merge(rDrives(n["ROA"], n["BusinessOperation"]), "Class", "name") # 资产收益率
    # graph.merge(rDrives(n["ROE"], n["BusinessOperation"]), "Class", "name") # 净资产收益率

    # ---Earning Analysis --- 增长因子
    # 利润增长
    graph.merge(rDrives(n["ProfitGrowth"], n["FundamentalPrice"]), "Class", "name")

    # ---Earning Analysis --- 现金流
    # graph.merge(rDrives(n["CashFlow"], n["Earning"]), "Class", "name")

    # ---Earning Analysis --- RedFlag

    # ---Valuation Analysis 估值因子
    graph.merge(rDrives(n["Earning"], n["FundamentalPrice"]), "Class", "name")
    graph.merge(rDrives(n["StockValuation"], n["FundamentalPrice"]), "Class", "name")


def BuildStockAnalysis(graph, n):
    #
    graph.merge(rDrives(n["AssetPrice"], n["Stock"]), "Class", "name")

    # ---Business Operation---
    graph.merge(rProfiling(n["Stock"], n["Corporation"]), "Class", "name")
    graph.merge(rSubordinate(n["Corporation"], n["Business"]), "Class", "name")

    # graph.merge(rSubordinate(n["Corporation"], n["RealEstate"]), "Class", "name")
    graph.merge(rSubordinate(n["Corporation"], n["Business"]), "Class", "name")

    graph.merge(rProfiling(n["Earning"], n["Corporation"]), "Class", "name")
    # graph.merge(rProfiling(n["BusinessOperation"], n["Corporation"]), "Class", "name")

    #
    graph.merge(rDrives(n["CompanyFinancing"], n["CostOfDebt"]), "Class", "name")
    graph.merge(rSubordinate(n["CostOfDebt"], n["CorpCost"]), "Class", "name")

    # --- Price = Earning X PE // Aka Price X Growth X Liquidity
    StockFundamentalPrice(graph, n)
    graph.merge(rDrives(n["FundamentalPrice"], n["Stock"]), "Class", "name")

    # ------------------------------------------------------------------------
    # 价格分析：动量与翻转
    graph.merge(rDrives(n["Momentum"], n["Stock"]), "Class", "name")

    # ------------------------------------------------------------------------
    # 狭义流动性分析：投资者结构
    graph.merge(rDrives(n["InvestorStructure"], n["Stock"]), "Class", "name")

    # ------------------------------------------------------------------------
    # 事件驱动
    graph.merge(rDrives(n["EventDriven"], n["Stock"]), "Class", "name")

    # ---Capital Restructure---
    graph.merge(rDrives(n["Restructure"], n["EventDriven"]), "Class", "name")
    graph.merge(rDrives(n["DividendStock"], n["EventDriven"]), "Class", "name")


def BuildFixIncomeAnalysis(graph, n):
    # ---Fix Income---
    graph.merge(rDrives(n["AssetPrice"], n["FixIncome"]), "Class", "name")


def BuildGlobalEconomy(graph, n):
    #
    graph.merge(rDrives(n["GlobalTrade"], n["Export"]), "Class", "name")
    graph.merge(rDrives(n["Globalization"], n["GlobalTrade"]), "Class", "name")
    graph.merge(rDrives(n["TradeConflict"], n["GlobalTrade"]), "Class", "name")

    graph.merge(rDrives(n["BeltAndRoad"], n["Export"]), "Class", "name")
    graph.merge(rDrives(n["BeltAndRoad"], n["RMBInternationalization"]), "Class", "name")

    #
    graph.merge(rDrives(n["RMBInternationalization"], n["RMB"]), "Class", "name")
    graph.merge(rDrives(n["SocialLiquidity"], n["RMB"]), "Class", "name")
    graph.merge(rDrives(n["Export"], n["RMB"]), "Class", "name")
    graph.merge(rDrives(n["USDX"], n["RMB"]), "Class", "name")
    graph.merge(rDrives(n["FXCapitalFlow"], n["RMB"]), "Class", "name")
    graph.merge(rDrives(n["FXRiskOnOff"], n["RMB"]), "Class", "name")
    #
    graph.merge(rDrives(n["FXRiskOnOff"], n["USDX"]), "Class", "name")
    graph.merge(rDrives(n["USEconomy"], n["USDX"]), "Class", "name")


# 利息调整场景
def DecreaseInsterstChain(graph, n):
    pass
    # n["DecreaseInterest"] = Node("Event", name="央行降息", english="Decrease Interest")
    # n["DecreaseInterest"] = Node("Event", name="信用扩张", english="Decrease Interest")
    # n["DecreaseInterest"] = Node("Event", name="实体间流动性增加", english="Decrease Interest")
    # n["DecreaseInterest"] = Node("Event", name="企业融资成本下降", english="Decrease Interest")
    # n["DecreaseInterest"] = Node("Event", name="工商业活动活跃", english="Decrease Interest")

    # ---消费驱动---
    graph.merge(rDrives(n["Consumption"], n["Industry_Auto"]), "Class", "name")
    graph.merge(rDrives(n["Consumption"], n["Industry_HomeAppliances"]), "Class", "name")

    # ---房地产行业---
    graph.merge(rDrives(n["CompanyFinancing"], n["Industry_RealEstate"]), "Class", "name")

    # ---金融行业---
    graph.merge(rDrives(n["BMRate"], n["Industry_Insurance"]), "Class", "name")
    graph.merge(rDrives(n["BMRate"], n["Industry_Trust"]), "Class", "name")
    graph.merge(rDrives(n["BMRate"], n["Industry_Securities"]), "Class", "name")

    # 央行逆周期操作 - 央行降息
    # 基准利率 - 基准利率下降
    # 银行 - 信用扩张
    # 实体间流动性 - 充裕
    # 资产价格 - 上升
    # 农产品 - 价格上升
    # 价格指数 - 上升
    # 牲畜 - 价格上升
    # 工业金属 - 价格上升
    # 股票 - 价格上升
    # 经济驱动：消费 - 上升
    # 企业融资成本 - 下降
    # 社会融资总额 - 上升
    # 债务成本 - 下降
    # 公司经营 - 改善
    # 公司盈利 - 上升
    # 利润增长 - 上升
    # 基本面价格 - 上升
    # 行业：银行 - 上升
    # 行业：房地产 - 上升
    # 经济驱动：房地产 - 上升
    # 经济 - 上升
    # 银行间市场货币供给 - 充裕
    # 银行超储率 - 上升
    # 短端利率 - 下降
    # 长端利率 - 下降
    # 人民币加权平均贷款利率 - 下降
    # 债券市场 - 价格上升
    # 人民币 - 汇率下降

    # 银行板块

    # 小微贷款量上升，利好零售银行巨头


def Load_StockInIndustry(pathfilename, graph):
    # ---Load Text File---
    book = xlrd.open_workbook(pathfilename)
    sheet = book.sheet_by_name("Sheet1")
    nrows = sheet.nrows

    for i in range(1, nrows):  # Including Header
        row_data = sheet.row_values(i)
        content = row_data
        print(content)
        #
        symbol = content[0]
        name = content[1]
        industry = content[3]
        #
        if industry not in n:
            industry = "Industry_" + industry
            if industry not in n:
                print("No Industry", industry)
                continue
        #
        node = Node("Entity", name=name, english=symbol)
        graph.merge(rInstanceOf(node, n[industry]), "Class", "name")


#graph = Graph("bolt://localhost:7687", username="neo4j", password="snow1")


# 得到结点
def GetNodes(graph, target_node, depth, label):
    cypher1 = "MATCH (a)-[*..depth]-(b) WHERE a.name = target_node RETURN b.name"
    node1 = graph.run(cypher1)
    list1 = []
    for i in node1:
        list1.append(str(i).split('=')[1][1:-2])
    print(list1)

    cypher2 = "MATCH (a:label) RETURN a.name"
    node2 = graph.run(cypher2)
    list2 = []
    for j in node2:
        list2.append(str(j).split('=')[1][1:-2])
    print(list2)

    get_nodes = list(set(list1) & set(list2))
    print(get_nodes)
    return get_nodes


#get_nodes = GetNodes(graph, target_node='中央银行', depth=5, label='Class')


# 逐步回归
def stepwise(df, y_node, x_nodes):
    y = df[y_node]
    x = df[x_nodes]

    def t_test(x_list):
        xTable = pd.DataFrame()
        for x_name in x_list:
            x_data = x[[x_name]]
            # 极值处理
            qt_left = x_data.dropna().quantile(0.05).values[0]  # 5%分位数
            qt_right = x_data.dropna().quantile(0.95).values[0]  # 95%分位数
            x_data[x_data < qt_left] = qt_left  # 小于5%分位数的取5%分位数
            x_data[x_data > qt_right] = qt_right  # 大于95%分位数的取95%分位数
            print(qt_left, qt_right)
            # 因子值标准化
            x_data = (x_data - np.mean(x_data)) / np.std(x_data)

            xTable = pd.concat([xTable, x_data], axis=1)  # 外连接x_nodes所有的因子值

        # 回归
        gls = sm.GLS(y, xTable).fit()
        output = pd.DataFrame(index=x_nodes, columns=['coef', 'pvalue'])

        for x_name in x_list:
            output.loc[x_name, 'coef'] = gls.params[x_name]
            output.loc[x_name, 'pvalue'] = gls.pvalues[x_name]

        return output

    # 对单因子做回归
    regression_table = pd.DataFrame()
    for factor_name in x_nodes:
        print('regression:', factor_name)
        regression_table = pd.concat([regression_table, t_test([factor_name])], axis=0)

    # 筛选通过t检验的因子并按绝对值排序
    pvalueTable_passed = regression_table[regression_table['pvalue'] < 0.05]  # 取出通过了t检验的因子
    if len(pvalueTable_passed) > 0:
        betaTable_passed = pvalueTable_passed.abs().sort_values(by='coef',
                                                                ascending=False).index.tolist()  # 把通过t检验的因子按照coef值从大到小排列

        # 逐步回归
        factorList_optimal = []
        for factor in betaTable_passed:
            print('stepwise:', factor)
            factorList_optimal.append(factor)
            outPut_data = t_test(factorList_optimal)
            if (outPut_data['pvalue'] > 0.1).sum() != 0:
                factorList_optimal.remove(factor)
        outPut_data = t_test(factorList_optimal)

    else:
        print('No Significant Factor!')
        outPut_data = pd.DataFrame()

    return outPut_data

import pandas as pd
import numpy as np
df = pd.read_csv('D:/index.csv', encoding='GBK').set_index('ind')
print(df.head())
outPut_data = stepwise(df, y_node='y', x_nodes=['x1','x2','x3','x4','x5','x6'])
print(outPut_data)
# BuildIndustryAnalysis(graph, n)
# Clear(graph)

# BuildBigPicture(graph, n)

# RealEstateAnalysis(graph, n)
# DecreaseInsterstChain(graph, n)

# Load_StockInIndustry("C:\\Users\\fengshimeng3\\Documents\\Astrolabe\\Industry Analysis.xlsx", graph)

# PrintGraph(graph)
# ExportToFile(graph, "d:\data\graph_update.txt")

# a = Node("Person", name="Alice")
# b = Node("Person", name="Bob")
# KNOWS = Relationship.type("KNOWS")
# graph.merge(KNOWS(a, b), "Person", "name") #

# MATCH (m)-[r:DRIVES]-(n)
# RETURN m.name,r.type,n.name